package com.dk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dk.mapper.UserMapper;
import com.dk.page.PageResult;
import com.dk.pojo.User;
import com.dk.pojo.UserExample;
import com.dk.service.Userservice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserserviceImpl implements Userservice {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void insertuser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAllById(List<String> ids) {
        userMapper.deleteAllById(ids);
    }

    @Override
    public PageResult<User> getAlluser(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list = userMapper.selectByExample(null);
        PageInfo<User> pageInfo=new PageInfo<>(list);
        return new PageResult<>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<User> getuserByName(String name) {
        UserExample example=new UserExample();
        if (!StrUtil.isEmpty(name)&&!"".equals(name)){
            example.createCriteria().andUnameLike("%"+name+"%");
        }
        List<User> list = userMapper.selectByExample(example);
        return list;
    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateuser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User LogingetuserByName(String username) {
        UserExample example=new UserExample();
        example.createCriteria().andUnameEqualTo(username);
        List<User> list = userMapper.selectByExample(example);
        if (list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
